package com.yicong.ants.ui.circle.home;

import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cchao.simplelib.Const;
import com.cchao.simplelib.core.AndroidHelper;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.circle.Tutorial;
import com.yicong.ants.databinding.TutorialIndexFragmentBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.ImageHelper;
import com.yicong.ants.manager.RouterHelper;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.find.YcWebViewActivity;
import com.yicong.ants.ui.other.FragmentContainerActivity;
import com.yicong.ants.utils.TimeHelper;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.Collections;


/**
 * 社区首页 tab
 *
 * @author cchao
 * @version 2019-07-31.
 */
public class TutorialIndexFragment extends SimpleLazyFragment<TutorialIndexFragmentBinding> implements View.OnClickListener {
    Tutorial mResp;

    @Override
    protected int getLayoutId() {
        return R.layout.tutorial_index_fragment;
    }

    @Override
    protected void initEventAndData() {
        mDataBind.banner.setAdapter(new BannerImageAdapter<Tutorial.BannerBean>(null) {
            @Override
            public void onBindView(BannerImageHolder holder, Tutorial.BannerBean data, int position, int size) {
                ImageHelper.loadImageRound(holder.imageView, data.getImg(), 4);
                holder.imageView.setOnClickListener(v -> {
                    //
                    RouterHelper.router(getActivity(), data.getUrl());
                });
            }
        }).addBannerLifecycleObserver(this)
            .setBannerGalleryEffect(10, 20)
            .setIndicator(new CircleIndicator(mContext));

        int height = (int) ((UiHelper.getScreenWidth() - (UiHelper.dp2px(16) * 3)) / 2f * (17 / 33.0f));
        mDataBind.bar1.getLayoutParams().height = height;
        mDataBind.bar2.getLayoutParams().height = height;
//        mDataBind.webView.loadUrl(Constants.getWebUrl("Trend"));
        mDataBind.swipeRefresh.setOnRefreshListener(() -> onLoadData());
    }

    @Override
    public void onFirstUserVisible() {
        mDataBind.setClick(this);
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        showProgress();
        addSubscribe(RetrofitHelper.getApis().tutorial(Collections.emptyMap())
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                mDataBind.swipeRefresh.setRefreshing(false);
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                mResp = respBean.getData();
                mDataBind.banner.setDatas(mResp.getSlide());
                mDataBind.setBean(mResp);
//                setChart();
//                updateLevel();
            }, throwable -> {
                hideProgress();
                Logs.logException(throwable);
                mDataBind.swipeRefresh.setRefreshing(false);
            }));
    }

    private void setProgressValue(ProgressBar progressBar, TextView levelText, float value) {
        if (value <= 0) {
            levelText.setText("虚位以待");
            levelText.setTextColor(UiHelper.getColor(R.color.text_bbb));
            progressBar.setProgress(0);
            progressBar.setProgressDrawable(UiHelper.getDrawable(R.drawable.progress_level_null));
            return;
        }

        if (value < mResp.getSubsidy().getMax_value() * 0.07) {
            progressBar.setProgressDrawable(UiHelper.getDrawable(R.drawable.progress_level_small));
        } else {
            progressBar.setProgressDrawable(UiHelper.getDrawable(R.drawable.progress_level));
        }
        progressBar.setMax(mResp.getSubsidy().getMax_value_int());
        progressBar.setProgress((int) (value * 100));
        levelText.setTextColor(UiHelper.getColor(R.color.colorPrimaryDark));
        levelText.setText(value + " 门票");
    }

    private void updateLevel() throws Exception {
        mDataBind.time.setText(TimeHelper.stampToFormat(String.valueOf(mResp.getSubsidy().getTimestamp() * 1000L)));

        Tutorial.Subsidy subsidy = mResp.getSubsidy();
        setProgressValue(mDataBind.level1Progress, mDataBind.level1Text, subsidy.getLevel_1_value());
        setProgressValue(mDataBind.level2Progress, mDataBind.level2Text, subsidy.getLevel_2_value());
        setProgressValue(mDataBind.level3Progress, mDataBind.level3Text, subsidy.getLevel_3_value());
        setProgressValue(mDataBind.level4Progress, mDataBind.level4Text, subsidy.getLevel_4_value());
        setProgressValue(mDataBind.level5Progress, mDataBind.level5Text, subsidy.getLevel_5_value());
    }

    @Override
    public void onClick(View v) {
        if (mResp == null) {
            return;
        }

        switch (v.getId()) {
            case R.id.portal_1:
                Router.turnTo(mContext, YcWebViewActivity.class)
                    .putExtra(Const.Extra.Web_View_Url, ZzConfigs.getWebUrl("faq").getUrl())
                    .putExtra(Const.Extra.Web_View_Tile, "常见问题")
                    .putExtra(Constants.Extra.Yc_Web_View_Can_Share, true)
                    .start();
                break;
            case R.id.portal_2:
                Router.turnTo(mContext, FragmentContainerActivity.class)
                    .putExtra(Constants.Extra.Fragment, Constants.Container.StarShare)
                    .putExtra(Constants.Extra.Title, "领导人分享")
                    .start();
                break;
            case R.id.portal_3:
                Router.turnTo(mContext, YcWebViewActivity.class)
                    .putExtra(Const.Extra.Web_View_Url, mResp.getStuff_url())
                    .putExtra(Const.Extra.Web_View_Tile, "课堂素材中心")
                    .putExtra(Constants.Extra.Yc_Web_View_Can_Share, true)
                    .start();
                break;
            case R.id.portal_4:
                Router.turnTo(mContext, YcWebViewActivity.class)
                    .putExtra(Const.Extra.Web_View_Title_Visible, false)
                    .putExtra(Const.Extra.Web_View_Url, mResp.getTravel_url())
                    .start();
                break;
            case R.id.quote:
                AndroidHelper.setClipboardData("【蚁丛语录】" + mResp.getQuote().getContent(), () -> {
                    //
                    showToast("复制成功");
                });
                break;
            case R.id.share:
                BusinessHelper.shareWx(getActivity()
                    , "蚁丛社区", Constants.Url.Favicon_Image
                    , mResp.getShare_url(), null);
                break;
        }
    }
}
